let CustomFilter = {}

import DateUtil from '@/mpf/date-util'



CustomFilter.install = function (Vue) {
    /**
	 * 日期格式化
	 */
	Vue.filter('date', (value, format) => {
		if(!value){
			return ''
		}
		format = format || 'yyyy-MM-dd'
		let date = DateUtil.toDate(value)
		return DateUtil.format(date, format)
	})

	/**
	 * 日期格式化
	 */
	Vue.filter('datetime', (value, format) => {
		if(!value){
			return ''
		}
		let date = DateUtil.toDate(value)
		format = format || 'yyyy-MM-dd HH:mm:ss'
		return DateUtil.format(date, format)
	})
	
	
	const PAY_CHANNEL = {
		'wxpay':"微信支付",
		'alipay':"支付宝"
	}
	
	/**
	 * 支付通道
	 */
	Vue.filter('payChannel', (value) => {
		console.log(value);
		if(!value){
			return ''
		}
		
		return PAY_CHANNEL[value];
		
	})
	
	
	
	/**
	 * 地址补全
	 */
	Vue.filter('url', (value, width, height) => {
		
		if(!value){
			return ''
		}
		
		if(value.indexOf('http') == 0){
			return value;
		}
		
		return  global.API_BASE_PATH + value;
	})
	
	/**
	 * 图片地址
	 */
	Vue.filter('image', (value, width, height) => {
		
		if(!value){
			return ''
		}
		
		if(value.indexOf('http') == 0){
			if(value.indexOf("?") == -1 && width && height){
				return value  + '?imageView2/1/w/'+ width + '/h/' + height;
			}
			
			
			return value;
		}
		
		if(value.indexOf('/static/images/') == 0){
			return value;
		}
		
		return  global.API_BASE_PATH + value;
	})
	
	
	/**
	 * 图片裁剪
	 */
	Vue.filter('thumb', (value, width, height) => {
		if(!value){
			return ''
		}
		
		if(value.indexOf('http') == 0){
			if(value.indexOf("?") == -1){
				if(width && height){
					return value  + '?imageView2/1/w/'+ width + '/h/' + height;
				}
				else if(width){
					return value  + '?imageView2/2/w/'+ width;
				}
				else{
					return value  + '?imageView2/2/w/'+ 750;
				}
				
			}
			
			
			
			return value;
		}
		
		if(value.indexOf('/static/images/') == 0){
			return value;
		}
		
		return  global.API_BASE_PATH + value;
	})
	
	
	/**
	 * 性别
	 */
	Vue.filter('gender', (value) => {
		if(!value){
			return ''
		}
		
		return value == 1 ? "男":"女";
	})
	
	/**
	 * 气泡值
	 */
	Vue.filter('badge', (value) => {
		if(value && value > 100){
			return '99+';
		}
		return value;
	});
	
	/**
	 * 富文本处理
	 */
	Vue.filter('richtext', (value, style) => {
		if(!value){
			return value;
		}
		value = value.replace(/<img[^>]+>/, function($1){
			let src = $1.replace(/.*src="(\/[^"]+)".*/, function($2, $3){
				return $3;
			});
			//  src="(\/[^"]+)
			console.log(src);
			if(!src.startsWith('http') && !src.startsWith('/static/images/')){
				if(src.startsWith('/prod-api')){
					src = src.substring(9)
				}
				else if(src.startsWith('/dev-api')){
					src = src.substring(8)
				}
				src =  global.API_BASE_PATH + src;
			}
			
			return '<img src="'+ src +'" style="max-width:100%;" />';
		})
		
		return value ;
	});
	
	Vue.filter('money', (value) => {
		if(!value){
			return value;
		}
		
		if(value > 10000){
			value = value / 10000;
			let str = value.toString();
			let dotPos = str.indexOf('.');
			if(dotPos > -1){
				let decimal = str.length - dotPos + 1;
				if(decimal > 2){
					return value.toFixed(2) + 'w';
				}
			}
			return value + 'w';
			
		}
		return value;
		
	});
	/**
	 * 设置小数位数
	 */
	Vue.filter('scale', (value, scale) => {
		if(!value){
			return value;
		}
		
		
		let str = value.toString();
		let dotPos = str.indexOf('.');
		if(dotPos > -1){
			let decimal = str.length - dotPos + 1;
			if(decimal > scale){
				return value.toFixed(scale);
			}
		}
		return value;
		
	})

	
}
export default CustomFilter
